#include "dominion.h"
#include <stdio.h>
#include "rngs.h"
#include <stdlib.h>

int main (int argc, char** argv) {

	struct gameState G;
	int k[10];

	SelectStream(2);
	PutSeed(2);
	
	int numPlayers = floor((Random() * 2) + 2);
	
	FILE *fp;
	fp = fopen("gameResults.out", "w");

	int randNum;
	char *supplyCard;

	fprintf(fp, "Starting game with ");
        fprintf(fp, "%i players\n", numPlayers);
	fprintf(fp, "Supply cards:\n");
	
	for (int i = 0; i < 10; i++) {
		randNum = floor(Random() * 20);
		if (randNum == 1) {
			k[i] = adventurer;
			supplyCard = "Adventurer";
		}
		else if (randNum == 2) {
			k[i] = council_room;
			supplyCard = "Council Room";
		}
		else if (randNum == 3) {
			k[i] = feast;
			supplyCard = "Feast";
		}
		else if (randNum == 4) {
			k[i] = gardens;
			supplyCard = "Gardens";
		}
		else if (randNum == 5) {
			k[i] = mine;
			supplyCard = "Mine";
		}
		else if (randNum == 6) {
			k[i] = remodel;
			supplyCard = "Remodel";
		}
		else if (randNum == 7) {
			k[i] = smithy;
			supplyCard = "Smithy";
		}
		else if (randNum == 8) {
			k[i] = village;
			supplyCard = "Village";
		}
		else if (randNum == 9) {
			k[i] = baron;
			supplyCard = "Baron";
		}
		else if (randNum == 10) {
			k[i] = great_hall;
			supplyCard = "Great Hall";
		}
		else if (randNum == 11) {
			k[i] = minion;
			supplyCard = "Minion";
		}
		else if (randNum == 12) {
			k[i] = steward;
			supplyCard = "Steward";
		}
		else if (randNum == 13) {
			k[i] = tribute;
			supplyCard = "Tribute";
		}
		else if (randNum == 14) {
			k[i] = ambassador;
			supplyCard = "Ambassador";
		}
		else if (randNum == 15) {
			k[i] = cutpurse;
			supplyCard = "Cutpurse";
		}
		else if (randNum == 16) {
			k[i] = embargo;
			supplyCard = "Embargo";
		}
		else if (randNum == 17) {
			k[i] = outpost;
			supplyCard = "Outpost";
		}
		else if (randNum == 18) {
			k[i] = salvager;
			supplyCard = "Salvager";
		}
		else if (randNum == 19) {
			k[i] = sea_hag;
			supplyCard = "Sea Hag";
		}
		else if (randNum == 20) {
			k[i] = treasure_map;
			supplyCard = "Treasure Map";
		}
		else {
			k[i] = adventurer;
			supplyCard = "Adventurer";
		}
		for (int j = 0; j < i; j++) {
			if (k[i] == k[j]) {
				i--;
			}
			else if (i - 1 == j) {
				fprintf(fp, "%s\n", supplyCard);
			}
		}
	}
	
	initializeGame(2, k, randNum, &G);

	int money, currPlayer;
	while(!isGameOver(&G)) {
		money = 0;
		currPlayer = whoseTurn(&G);
		fprintf(fp, "Player %i has:\n1 action(s)\n1 buy(s)\n%i coin(s)\n", currPlayer, money);
		for (int i = 0; i < numHandCards(&G); i++) {
			if (handCard(i, &G) == copper) {
				money++;
			}
			else if (handCard(i, &G) == silver) {
				money += 2;
			}
			else if (handCard(i, &G) == gold) {
				money += 3;
			}
		}
		fprintf(fp, "Player %i plays all coins from their hand\n", currPlayer);
		fprintf(fp, "Player %i has:\n1 action(s)\n1 buy(s)\n%i coin(s)\n", currPlayer, money);

		if (money >= 8) {
			fprintf(fp, "Player %i buys Province\n", currPlayer);
			buyCard(province, &G);
		}
		else if (money >= 6) {
			fprintf(fp, "Player %i buys Gold\n", currPlayer);
                        buyCard(gold, &G);
		}
		else if (money >= 3) {
			fprintf(fp, "Player %i buys Silver\n", currPlayer);
                        buyCard(silver, &G);
		}
		fprintf(fp, "Player %i ends their turn\n", currPlayer);
		endTurn(&G);
	}

	fprintf(fp, "Finished Game\n");
	if (numPlayers == 2) {
		fprintf(fp, "Player 1's score: %d\nPlayer 2's score: %d\n", scoreFor(0, &G), scoreFor(1, &G));
	}
	else if (numPlayers == 3) {
		fprintf(fp, "Player 1's score: %d\nPlayer 2's score: %d\nPlayer 3's score: %d\n", scoreFor(0, &G), scoreFor(1, &G), scoreFor(2, &G));
	}
	else if (numPlayers == 4) {
		fprintf(fp, "Player 1's score: %d\nPlayer 2's score: %d\nPlayer 3's score: %d\nPlayer 4's score: %d\n", scoreFor(0, &G), scoreFor(1, &G), scoreFor(2, &G), scoreFor(3, &G));
	}
	
	fclose(fp);
}
